package com.jackrain.nea.ad.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.parser.Feature;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.jackrain.nea.ad.api.AdmonitorQueryCmd;
import com.jackrain.nea.ad.mapper.AdmonitorMapper;
import com.jackrain.nea.exception.NDSException;
import com.jackrain.nea.sys.CommandAdapter;
import com.jackrain.nea.util.ApplicationContextHandle;
import com.jackrain.nea.util.ValueHolder;
import com.jackrain.nea.web.query.DefaultWebEvent;
import com.jackrain.nea.web.query.QuerySession;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;

@Slf4j
@Component
@Service(protocol = "dubbo", validation = "true", version = "1.0", group = "ad")
public class AdmonitorQueryCmdImpl extends CommandAdapter implements AdmonitorQueryCmd {

    @Override
    public ValueHolder execute(QuerySession querySession) throws NDSException {
        ValueHolder vh = new ValueHolder();
        AdmonitorMapper admonitorMapper = ApplicationContextHandle.getBean(AdmonitorMapper.class);
        DefaultWebEvent event = querySession.getEvent();
        JSONObject param = JSON.parseObject(JSON.toJSONStringWithDateFormat(event.getParameterValue("param"), "yyyy-MM-dd HH:mm:ss", SerializerFeature.WriteMapNullValue), Feature.OrderedField);
        Long id = param.getLong("ID");
        Long admenuid= param.getLong("AD_MENU_ID");

        JSONObject jsonObject = new JSONObject();
        JSONObject checkjo = new JSONObject();
        JSONObject data = new JSONObject();
        if (admenuid != null && admenuid > 0) {
            HashMap selectMenuType = admonitorMapper.selectMenuType(admenuid);
            if ((Long) selectMenuType.get("TYPE") == 1) {
                //1查询table
                String tableName = admonitorMapper.selectTableName((Long) selectMenuType.get("AD_TABLE_ID"));
                if (id != -1 && id != null) {
                    jsonObject.put("AD_MENU_ID:NAME", tableName);
                }
                //查询mask
                String selectMask = admonitorMapper.selectTableMask((Long) selectMenuType.get("AD_TABLE_ID"));
                //获取每一位
                char[] c = selectMask.toCharArray();
                JSONArray tablejoArray = new JSONArray();
                int s=0;
                for (int i = 0; i < c.length; i++) {
                    if ('Q' != c[i]&&'B'!=c[i]) {
                        tablejoArray.add(s, c[i]);
                        s++;
                    }
                }

                //查询和表id有关联的action且不等与标签的动作定义
                List<HashMap> selectActionInfo = admonitorMapper.selectActionInfo((Long) selectMenuType.get("AD_TABLE_ID"));
                JSONArray actionjoArray = new JSONArray();
                for (int i = 0; i < selectActionInfo.size(); i++) {
                    JSONObject actionJo = new JSONObject();
                    //获取每个不是标签动作定义放入action
                    actionJo.put("id", selectActionInfo.get(i).get("ID"));
                    actionJo.put("desc", selectActionInfo.get(i).get("DESCRIPTION"));
                    actionjoArray.add(i, actionJo);
                }

                //查询所有的在这个表下的动作定义
                List<HashMap> selectAllActionInfo = admonitorMapper.selectAllActionInfo((Long) selectMenuType.get("AD_TABLE_ID"));
                JSONArray actionSelectArray = new JSONArray();
                int sum = 0;
                for (int i = 0; i < selectAllActionInfo.size(); i++) {
                    //查询是否该其他动作定义在该动作定义下
                    List<HashMap> selectActionChildren = admonitorMapper.selectActionChildren((Long) selectAllActionInfo.get(i).get("ID"));
                    JSONObject fatherJo = new JSONObject();
                    //如果有其他的动作定义且不等标签的放入
                    if (selectActionChildren != null && selectActionChildren.size() > 0) {
                        //放入父级动作定义
                        fatherJo.put("id", selectAllActionInfo.get(i).get("ID"));
                        fatherJo.put("desc", selectAllActionInfo.get(i).get("DESCRIPTION"));
                        JSONArray childrenArray = new JSONArray();
                        for (int j = 0; j < selectActionChildren.size(); j++) {
                            JSONObject childrenJo = new JSONObject();
                            childrenJo.put("id", selectActionChildren.get(j).get("ID"));
                            childrenJo.put("desc", selectActionChildren.get(j).get("DESCRIPTION"));
                            //放入子级动作定义
                            childrenArray.add(j, childrenJo);
                        }
                        fatherJo.put("children", childrenArray);
                        actionSelectArray.add(sum, fatherJo);
                    }
                }

                checkjo.put("table", tablejoArray);
                checkjo.put("action", actionjoArray);
                checkjo.put("actionSelect", actionSelectArray);
            } else if ((Long) selectMenuType.get("TYPE") == 2) {
                //2查询action
                String actionName = admonitorMapper.selectActionName((Long) selectMenuType.get("AD_ACTION_ID"));
                if (id != -1 && id != null) {
                    jsonObject.put("AD_MENU_ID:NAME", actionName);
                }
                //查询该actionId在Action表中是表id
                HashMap tablehash = admonitorMapper.selectCategoryOrTableid((Long) selectMenuType.get("AD_ACTION_ID"));
                JSONArray tablejoArray = new JSONArray();
                JSONArray actionSelectArray = new JSONArray();
                JSONArray actionjoArray = new JSONArray();
                if(tablehash.get("AD_TABLE_ID")!=null&&(Long)tablehash.get("AD_TABLE_ID")>0){
                    String selectMask = admonitorMapper.selectTableMask((Long) tablehash.get("AD_TABLE_ID"));
                    //获取每一位
                    char[] c = selectMask.toCharArray();
                    int s=0;
                    for (int i = 0; i < c.length; i++) {
                        if ('Q' != c[i]&&'B'!=c[i]) {
                            tablejoArray.add(s, c[i]);
                            s++;
                        }
                    }

                    //查询和表id有关联的action且不等与标签的动作定义
                    List<HashMap> selectActionInfo = admonitorMapper.selectActionInfo((Long) tablehash.get("AD_TABLE_ID"));
                    for (int i = 0; i < selectActionInfo.size(); i++) {
                        JSONObject actionJo = new JSONObject();
                        //获取每个不是标签动作定义放入action
                        actionJo.put("id", selectActionInfo.get(i).get("ID"));
                        actionJo.put("desc", selectActionInfo.get(i).get("DESCRIPTION"));
                        actionjoArray.add(i, actionJo);
                    }

                    //查询所有的在这个表下的动作定义
                    List<HashMap> selectAllActionInfo = admonitorMapper.selectAllActionInfo((Long) tablehash.get("AD_TABLE_ID"));
                    int sum = 0;
                    for (int i = 0; i < selectAllActionInfo.size(); i++) {
                        //查询是否该其他动作定义在该动作定义下
                        List<HashMap> selectActionChildren = admonitorMapper.selectActionChildren((Long) selectAllActionInfo.get(i).get("ID"));
                        JSONObject fatherJo = new JSONObject();
                        //如果有其他的动作定义且不等标签的放入
                        if (selectActionChildren != null && selectActionChildren.size() > 0) {
                            //放入父级动作定义
                            fatherJo.put("id", selectAllActionInfo.get(i).get("ID"));
                            fatherJo.put("desc", selectAllActionInfo.get(i).get("DESCRIPTION"));
                            JSONArray childrenArray = new JSONArray();
                            for (int j = 0; j < selectActionChildren.size(); j++) {
                                JSONObject childrenJo = new JSONObject();
                                childrenJo.put("id", selectActionChildren.get(j).get("ID"));
                                childrenJo.put("desc", selectActionChildren.get(j).get("DESCRIPTION"));
                                //放入子级动作定义
                                childrenArray.add(j, childrenJo);
                            }
                            fatherJo.put("children", childrenArray);
                            actionSelectArray.add(sum, fatherJo);
                        }
                    }
                }else{
                    //查询Action表中其他在此action下的id
                    List<HashMap> selectObjaction=admonitorMapper.selectActionChildren((Long) selectMenuType.get("AD_ACTION_ID"));
                    for(int i=0;i<selectObjaction.size();i++){
                        JSONObject actionJo = new JSONObject();
                        //获取每个不是标签动作定义放入action
                        actionJo.put("id", selectObjaction.get(i).get("ID"));
                        actionJo.put("desc", selectObjaction.get(i).get("DESCRIPTION"));
                        actionjoArray.add(i, actionJo);
                    }

                    //查询所有actionId下的标签定义
                    List<HashMap> selectAllActionByActionId =admonitorMapper.selectActionByActionId((Long) selectMenuType.get("AD_ACTION_ID"));
                    int sum = 0;
                    for (int i = 0; i < selectAllActionByActionId.size(); i++) {
                        //看他们下面是否还有其他标签定义
                        List<HashMap> selectActionChildren = admonitorMapper.slectActionbyActionId((Long) selectAllActionByActionId.get(i).get("ID"));
                        if (selectActionChildren != null && selectActionChildren.size() > 0) {
                            JSONObject outjo = new JSONObject();
                            outjo.put("id", selectAllActionByActionId.get(i).get("ID"));
                            outjo.put("desc", selectAllActionByActionId.get(i).get("DESCRIPTION"));
                            JSONArray childrenArray = new JSONArray();
                            for (int j = 0; j < selectActionChildren.size(); j++) {
                                JSONObject childrenJo = new JSONObject();
                                childrenJo.put("id", selectActionChildren.get(j).get("ID"));
                                childrenJo.put("desc", selectActionChildren.get(j).get("DESCRIPTION"));
                                //放入子级动作定义
                                childrenArray.add(j, childrenJo);
                            }
                            outjo.put("children", childrenArray);
                            actionSelectArray.add(sum, outjo);
                        }
                    }
                }

                checkjo.put("table",tablejoArray);
                checkjo.put("action", actionjoArray);
                checkjo.put("actionSelect", actionSelectArray);
            }
        }

//        if (id != -1 && id != null) {
//            HashMap selectMonitorInfo = admonitorMapper.selectMonitorInfo(id);
//            jsonObject.put("AD_MENU_ID", selectMonitorInfo.get("AD_MENU_ID"));
//            jsonObject.put("NAME", selectMonitorInfo.get("NAME"));
//            jsonObject.put("PROPS", JSON.parseObject((String) selectMonitorInfo.get("PROPS")));
//            jsonObject.put("ISACTIVE", selectMonitorInfo.get("ISACTIVE"));
//        }

        data.put("value", jsonObject);
        data.put("checkActionsAll", checkjo);
        vh.put("code", 0);
        vh.put("data", data);
        return vh;
    }
}
